package com.rutas.java;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;

public class DownloadXml extends AsyncTask<String, String, Boolean>{
	private ProgressDialog dialogo;
	private Context miContexto;
	private ArrayList<String> xml;
	private static final String HOST = "http://javi-practice.googlecode.com/files/";
	
	public DownloadXml(final Context contexto, final ArrayList<String> datos){
		miContexto = contexto;
		xml = datos;
	}
	
	@Override
	protected void onPreExecute(){
		
		dialogo = new ProgressDialog(miContexto);
		dialogo.setMessage(miContexto.getString(R.string.espere));
		dialogo.setTitle(miContexto.getString(R.string.descargando_rutas));
		dialogo.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialogo.setIcon(android.R.drawable.ic_popup_sync);
		dialogo.setCancelable(false);
		dialogo.setMax(xml.size());
		dialogo.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialogo.dismiss();
			}
		});
		dialogo.show();
		dialogo.getButton(AlertDialog.BUTTON_NEUTRAL).setEnabled(false);
	}

	@Override
	protected Boolean doInBackground(String... direc) {
		URL direccion;
		int count = xml.size();
		for (int i = 0; i < count; i++){
		try {
				direccion = new URL(HOST + xml.get(i));
				URLConnection conexion = direccion.openConnection();
				InputStream myInput = new BufferedInputStream (conexion.getInputStream());
				
				//File rutas = new File(Environment.getExternalStorageDirectory(), xml.get(i));
				File rutas = new File("/data/data/com.rutas.java/files/", xml.get(i));
				//rutas.mkdirs();
				if (!rutas.exists()){
					FileOutputStream myOutput = new FileOutputStream(rutas);
					//transfer bytes from the inputfile to the outputfile
			    	byte[] buffer = new byte[1024];
			    	int length;
			    	while ((length = myInput.read(buffer)) > 0){
			    		myOutput.write(buffer, 0, length);
			    	}
			    	myOutput.flush();
			    	myOutput.close();
				}
			    myInput.close();
		    	publishProgress(xml.get(i));
			
		}catch	(MalformedURLException e) {
				//TODO en caso de error cerrar flujos
				e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
		}//end for
		return true;
	}
	
	@Override
	protected void onProgressUpdate(String... progress) {
		dialogo.setMessage(progress[0]);
		dialogo.incrementProgressBy(1);
	}
	
	@Override
	protected void onPostExecute(Boolean hecho){
		dialogo.setMessage(miContexto.getString(R.string.actualizacion_terminada));
		dialogo.getButton(AlertDialog.BUTTON_NEUTRAL).setEnabled(true);
	}
}
